﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication9.Models;

namespace MvcApplication9.Controllers
{
    public class HomeController : Controller
    {
        private MainModelContainer db = new MainModelContainer();

        private int PageSize = 2;

        public ActionResult Index()
        {
            /*Excursion e = new Excursion { Subject = "E 1", Description = "D 1", FreeOfCharge = false, Price = 12, TStart = new DateTime(1999,1,1) };
            Excursion e2 = new Excursion { Subject = "E 2", Description = "D 2", FreeOfCharge = false, Price = 12, TStart = new DateTime(1999, 1, 1) };
            Excursion e3 = new Excursion { Subject = "E 3", Description = "D 3", FreeOfCharge = false, Price = 12, TStart = new DateTime(1999, 1, 1) };
            var point = (CheckPoint)(from i in db.CheckPoints where i.Id==20 select i).First();
            point.Excursion.Add(e);
            point.Excursion.Add(e2);
            point.Excursion.Add(e3);
            db.SaveChanges();*/
            return View();
        }

        public ActionResult Ships()
        {
            var ships = from i in db.Ships select i;
            return View(ships.ToList());
        }

        public ActionResult Cruises(string sorting, string filter_points, string sorting_date,string date, int page = 1)
        {
            if (sorting == null)
            {
                sorting = "Время";
            }
            var AllPoints = from i in db.CheckPoints orderby i.Title select i.Title;
            var pointslist = new List<string>();
            pointslist.AddRange(AllPoints.Distinct());
            ViewBag.filter_points = new SelectList(pointslist);
            
            var sortby = new List<string> { "Время", "Имя", "Цена" };
            ViewBag.sorting = new SelectList(sortby);
            
            var cr = from i in db.Ships select i;

            switch (sorting)
            {
                case "Имя":
                    cr = cr.OrderBy(x => x.Course.Title);
                    break;
                case "Цена":
                    cr = cr.OrderBy(x => x.Cost.Payment);
                    break;
                case "Время":
                    cr = cr.OrderBy(x => x.Survey);
                    break;
            }
            if (!string.IsNullOrEmpty(filter_points))
                cr = cr.Where(x => x.Course.CoursePoint.Any(course_points => course_points.CheckPoint.Title == filter_points));
            if (date != null)
            {
                string hhh = date;
                DateTime ddd = DateTime.Parse(hhh);
                cr = from i in cr where i.Survey == ddd select i;
                //return Redirect("~/Home/Cruises?date="+date);
            }
            else
            {
                string hhh = "2009/3/8";
                DateTime ddd = DateTime.Parse(hhh);
                cr = from i in cr where i.Survey != ddd select i;
            }
            
            //cr = cr.Where(x => x.Survey == new DateTime(2009, 3, 8));
            ViewBag.PagesCount = (int)Math.Ceiling((double)cr.Count() / PageSize);
            ViewBag.sortingValue = sorting;
            ViewBag.filter_pointsValue = filter_points;
            ViewBag.CurrentPage = page;
            //return View(cr.Skip((page - 1) * PageSize).Take(PageSize).ToList());
            return View(cr.ToList());
        }

        
        public ActionResult CruiseInfo(int ship_id,string comments,int? comment_id,int page=1)
        {
            if (comment_id != null)
            {
                Comment com =(Comment) (from i in db.Comments where i.Id == comment_id select i).Single();
                db.Comments.DeleteObject(com);
                db.SaveChanges();
            }
            if (!string.IsNullOrEmpty(comments))
            {
                Comment com = new Comment { Date = DateTime.Now, Login = "", Text = comments };
                db.Comments.AddObject(com);
                db.SaveChanges();
            }
            var ship = db.Ships.FirstOrDefault(x => x.Id == ship_id);
            ViewBag.Comments = (from i in db.Comments orderby i.Date descending select i).Skip((page - 1) * PageSize).Take(PageSize);
            ViewBag.Comments_Count = (int)Math.Ceiling((double)db.Comments.Count()/PageSize);
            return View(ship);
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult InfoAboutPoint(int id)
        {
            var info = db.CheckPoints.FirstOrDefault(x => x.Id == id);
            return View(info);
        }

        public ActionResult InfoAboutExcursion(int exc_id)
        {
            var exc = db.Excursions.FirstOrDefault(x => x.Id == exc_id);
            return View(exc);
        }

        public ActionResult InfoAboutShip(int ship_id)
        {
            var ship = db.Ships.FirstOrDefault(x => x.Id == ship_id);
            return View(ship);
        }

        public ActionResult Admin()
        {
            return View();
        }

        public ActionResult Submit()
        {
            var submits = from i in db.Submits orderby i.SubmitDate descending select i;

            return View(submits.ToList());
        }
    }
}
